import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../../resource/language/generated/l10n.dart';
import '../create/room_create_page.dart';
import 'my_room_page_model.dart';

class MyRoomPage extends StatefulWidget {
  static void showWithBottomSheet(BuildContext context) async {
    /// 获取我的房间列表
    // var result = await RtcApi.instance.getMyRooms();
    // if (result != null && result.data != null) {
    //   if (result.data!.lobbyList.length == 1 &&
    //       result.data!.lobbyList[0].usage == RoomUsage.COMMON) {
    //     RoomPage.show(context, result.data!.lobbyList[0].channelId.toInt());
    //     return;
    //   } else if (result.data!.lobbyList.isEmpty) {
    //     await RoomCreatePage.showWithBottomSheet(context);
    //     return;
    //   }
    // }
    await showModalBottomSheet(
        isScrollControlled: true,
        shape: const RoundedRectangleBorder(
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(16),
            topRight: Radius.circular(16),
          ),
        ),
        context: context,
        builder: (BuildContext context) {
          //构建弹框中的内容
          return MyRoomPage(
              // dataFromParam: result.data,
              );
        });
  }

  // GetLobbyListRsp? dataFromParam;

  MyRoomPage({Key? key}) : super(key: key);

  @override
  State<MyRoomPage> createState() => _MyRoomPageState();
}

class _MyRoomPageState extends State<MyRoomPage> {
  @override
  Widget build(BuildContext context) {
    print("my_room_page containCommon ");
    return MultiProvider(
        providers: [
          ChangeNotifierProvider(create: (_) => MyRoomPageModel()),
        ],
        child: Consumer<MyRoomPageModel>(
            builder: (BuildContext context, MyRoomPageModel model, child) {
          // int count = model.data.lobbyList.length;
          // bool containCommon = model.data.lobbyList
          //     .any((element) => element.usage == RoomUsage.COMMON);

          int count = 0;
          // bool containCommon = model.data.lobbyList
          //     .any((element) => element.usage == RoomUsage.COMMON);

          // print("my_room_page containCommon ${containCommon}");
          // if (!containCommon) {
          /// 如果没有自己创建的房间，增加一个add按钮
          count += 1;
          // }
          return AnimatedPadding(
              padding: MediaQuery.of(context).viewInsets,
              duration: const Duration(milliseconds: 100),
              child: Container(
                  decoration: const BoxDecoration(
                    color: Color(0xFFFFFFFF),
                    borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(16),
                      topRight: Radius.circular(16),
                    ),
                  ),
                  height: 464,
                  padding: const EdgeInsets.only(top: 20, bottom: 20),
                  child: Column(mainAxisSize: MainAxisSize.min, children: [
                    Text(
                      T.current!.my_room,
                      style: const TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.w500,
                        color: Color(0xff333333),
                      ),
                    ),
                    Expanded(
                      child: ListView.builder(
                          shrinkWrap: true,
                          itemCount: count,
                          physics: const ClampingScrollPhysics(),
                          itemBuilder: (context, index) {
                            // if (!containCommon && index == count - 1) {
                            //   return _addWidget(context, model);
                            // }
                            const Text('MyRoomItem');
                            // return MyRoomItem(
                            //   model.data.lobbyList[index],
                            //   backgroundColor: const Color(0xFFF6F7F9),
                            // );
                          }),
                    )
                  ])));
        }));
  }

  Widget _addWidget(BuildContext context, MyRoomPageModel model) {
    return CupertinoButton(
      onPressed: () async {
        await RoomCreatePage.showWithBottomSheet(context);
        model.initData();
      },
      minSize: 0,
      padding: EdgeInsets.zero,
      child: Container(
          margin: const EdgeInsets.only(left: 24, right: 24, top: 16),
          padding: const EdgeInsets.all(12),
          decoration: BoxDecoration(
            color: const Color(0xFFF6F7F9),
            borderRadius: BorderRadius.circular(16),
          ),
          height: 100,
          width: double.infinity,
          alignment: Alignment.center,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Image.asset(
                "assets/images/ic_room_add.png",
                width: 21,
                height: 21,
                color: const Color(0xff999999),
              ),
              const SizedBox(
                height: 9,
              ),
              Text(
                T.current!.create_room,
                style: const TextStyle(
                  fontSize: 15,
                  fontWeight: FontWeight.w500,
                  color: Color(0xff999999),
                ),
              )
            ],
          )),
    );
  }
}
